package com.hh.system.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.hh.baseframework.util.Check;
import com.hh.baseframework.util.Convert;
import com.hh.baseframework.util.FileUtil;
import com.hh.baseframework.util.Json;
import com.hh.baseframework.util.base.BaseAction;
import com.hh.baseframework.util.document.FileUpload;
import com.hh.baseframework.util.statics.StaticVar;

@SuppressWarnings("serial")
public class ActionFile extends BaseAction {
	private byte[] bytes = null;
	private String name;
	private File attachment;
	private String attachmentFileName;
	private String filePath;
	private String params;

	public void save() {
		response.setContentType("text/html");
		String path = FileUpload.uploadFile(attachment, attachmentFileName,
				filePath);
		Map<String, String> returnMap = new HashMap<String, String>();
		returnMap.put("path", path);
		returnMap.put("attachmentFileName", attachmentFileName);
		returnMap.put("name", name);
		this.getResultMap().putAll(returnMap);
		this.returnResult();
	}

	public String download() {
		Map<String, Object> map = Json.toMap(params);

		this.setFilePath(Convert.toString(map.get("filePath")));
		this.setName(Convert.toString(map.get("name")));
		if (Check.isEmpty(this.getName())) {
			this.setName(filePath.substring(filePath.lastIndexOf("/")));
		}
		File file = new File(StaticVar.filepath + filePath);
		if (file != null) {
			try {
				this.setBytes(FileUtil.stream2Byte(new FileInputStream(file)));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		return "file";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public File getAttachment() {
		return attachment;
	}

	public void setAttachment(File attachment) {
		this.attachment = attachment;
	}

	public String getAttachmentFileName() {
		return attachmentFileName;
	}

	public void setAttachmentFileName(String attachmentFileName) {
		this.attachmentFileName = attachmentFileName;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public byte[] getBytes() {
		return bytes;
	}

	public void setBytes(byte[] bytes) {
		this.bytes = bytes;
	}

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}


}
